package org.ruoyi.graph2.service.impl;

import org.ruoyi.common.core.utils.MapstructUtils;
    import org.ruoyi.core.page.TableDataInfo;
    import org.ruoyi.core.page.PageQuery;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.ruoyi.graph2.domain.GraphBase;
import org.ruoyi.graph2.domain.bo.GraphBaseBo;
import org.ruoyi.graph2.domain.vo.GraphBaseVo;
import org.ruoyi.graph2.mapper.GraphBaseMapper;
import org.ruoyi.graph2.service.IGraphBaseService;
import org.springframework.stereotype.Service;
import org.ruoyi.common.core.utils.StringUtils;

import java.util.List;
import java.util.Collection;

/**
 * 图谱基础信息表Service业务层处理
 *
 * @author ageerle
 * @date Tue Nov 18 15:28:04 CST 2025
 */
@RequiredArgsConstructor
@Service
public class GraphBaseServiceImpl implements IGraphBaseService {

    private final GraphBaseMapper baseMapper;

    /**
     * 查询图谱基础信息表
     */
    @Override
    public GraphBaseVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

	/**
	 * 查询图谱基础信息表列表
	 */
	@Override
	public TableDataInfo<GraphBaseVo> queryPageList(GraphBaseBo bo, PageQuery pageQuery) {
		LambdaQueryWrapper<GraphBase> lqw = buildQueryWrapper(bo);
		Page<GraphBaseVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
		return TableDataInfo.build(result);
	}

    /**
     * 查询图谱基础信息表列表
     */
    @Override
    public List<GraphBaseVo> queryList(GraphBaseBo bo) {
        LambdaQueryWrapper<GraphBase> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<GraphBase> buildQueryWrapper(GraphBaseBo bo) {
        LambdaQueryWrapper<GraphBase> lqw = Wrappers.lambdaQuery();
                    lqw.eq(bo.getId() != null, GraphBase::getId, bo.getId());
                    lqw.eq(StringUtils.isNotBlank(bo.getTenantId()), GraphBase::getTenantId, bo.getTenantId());
                    lqw.eq(StringUtils.isNotBlank(bo.getGraphGroup()), GraphBase::getGraphGroup, bo.getGraphGroup());
                    lqw.eq(StringUtils.isNotBlank(bo.getGraphTitle()), GraphBase::getGraphTitle, bo.getGraphTitle());
                    lqw.eq(bo.getTotalNodes() != null, GraphBase::getTotalNodes, bo.getTotalNodes());
                    lqw.eq(bo.getTotalRelationships() != null, GraphBase::getTotalRelationships, bo.getTotalRelationships());
                    lqw.eq(StringUtils.isNotBlank(bo.getNodeTypeDistribution()), GraphBase::getNodeTypeDistribution, bo.getNodeTypeDistribution());
                    lqw.eq(StringUtils.isNotBlank(bo.getRelationTypeDistribution()), GraphBase::getRelationTypeDistribution, bo.getRelationTypeDistribution());
                    lqw.eq(bo.getCreateBy() != null, GraphBase::getCreateBy, bo.getCreateBy());
                    lqw.eq(bo.getCreateTime() != null, GraphBase::getCreateTime, bo.getCreateTime());
                    lqw.eq(bo.getUpdateBy() != null, GraphBase::getUpdateBy, bo.getUpdateBy());
                    lqw.eq(bo.getUpdateTime() != null, GraphBase::getUpdateTime, bo.getUpdateTime());
        return lqw;
    }

    /**
     * 新增图谱基础信息表
     */
    @Override
    public Boolean insertByBo(GraphBaseBo bo) {
        GraphBase add = MapstructUtils.convert(bo, GraphBase. class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改图谱基础信息表
     */
    @Override
    public Boolean updateByBo(GraphBaseBo bo) {
        GraphBase update = MapstructUtils.convert(bo, GraphBase. class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(GraphBase entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除图谱基础信息表
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
